use std::sync::LazyLock;

mod hanyu;
mod wade_giles;

static HANZI_TRANSLATOR: LazyLock<hanyu::HanziToPinyinTranslator> =
    LazyLock::new(hanyu::read_hanzi_resource);
static WG_TRANSLATOR: LazyLock<wade_giles::WadeGilesTranslator> =
    LazyLock::new(wade_giles::read_wade_resource);

pub fn converter(input: &str) -> String {
    let ans = input
        .chars()
        .map(|ch| translate(ch).unwrap_or("")) //单字无法翻译就返回空
        .collect::<Vec<&str>>()
        .join(" ");

    println!("input: {input}, ans: {ans}");

    if ans.is_empty() {
        return ans;
    }

    // 首字母大写
    let mut chars = ans.chars();
    let first = chars.next().unwrap().to_uppercase();
    let rest: String = chars.collect();
    format!("{}{}", first, rest)
}

/// 把单个汉字翻译成威妥玛.
/// 无法翻译时返回None.
fn translate(hanzi: char) -> Option<&'static str> {
    let pinyin = HANZI_TRANSLATOR.trans_hanzi_simple(hanzi)?;
    WG_TRANSLATOR.trans_pinyin(pinyin)
}
